package blog;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import utilities.ServletUtilities;

import db.BeanUtilities;
import db.Blog;
import db.HibernateUtil;
import db.Users;

/**
 * Servlet implementation class Blog
 */
@WebServlet("/Blog")
public class BlogAdmin extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BlogAdmin() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Boolean isAdmin = ServletUtilities.isAdmin(request);
		if (isAdmin) {
			if(request.getParameter("idDelete") != null) {
				doDelete(request, response, Integer.parseInt(request.getParameter("idDelete")));
			} else if(request.getParameter("idEdit") != null) {
				doEdit(request, response, Integer.parseInt(request.getParameter("idEdit")));
			} else {
				response.sendRedirect("/runWithJava/admin_main.jsp");
			}
		} else {
			HttpSession session = request.getSession(true);
			session.setAttribute("error", "Dostep zabrioniony");
			response.sendRedirect("/runWithJava/");
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Boolean isAdmin = ServletUtilities.isAdmin(request);
		if (isAdmin) {
			Blog blog = new Blog();
			BeanUtilities.populateBean(blog, request.getParameterMap());
		    HibernateUtil<Blog> hu = new HibernateUtil<Blog>();
		    try{
		    	hu.create(blog);
		    	response.sendRedirect("/runWithJava/admin_main.jsp");
		    } catch(Exception e) {
		    	  System.out.println(e);
			      HttpSession session = request.getSession(true);
			      session.setAttribute("error", e.getMessage());
			      session.setAttribute("errorObject", blog);
			      response.sendRedirect("/runWithJava/admin_blog.jsp");      
		    }
		} else {
			response.sendError(500);
		}  
	}

	protected void doDelete(HttpServletRequest request, HttpServletResponse response, int id) throws ServletException, IOException {
		BlogHome.removeArticle(id);
		response.sendRedirect("/runWithJava/admin_main.jsp");
	}
	
	protected void doEdit(HttpServletRequest request, HttpServletResponse response, int id) throws ServletException, IOException {
		Blog blog = new Blog();
		BeanUtilities.populateBean(blog, request.getParameterMap());
		BlogHome.editArticle(blog, id);
		response.sendRedirect("/runWithJava/admin_main.jsp");
	}
}
